package com.amigo.online.provider.user.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.amigo.online.common.base.security.LocalUser;
import com.amigo.online.common.param.RedisKey;
import com.amigo.online.common.param.result.HobbyInfo;
import com.amigo.online.common.param.result.HobbyUserInfo;
import com.amigo.online.common.param.result.LocalUserInfo;
import com.amigo.online.common.param.result.UserInfo;
import com.amigo.online.provider.user.common.GlobalController;
import com.amigo.online.provider.user.common.RwRedisService;
import com.amigo.online.provider.user.mapper.HobbyMapper;
import com.amigo.online.provider.user.mapper.UserHobbyMapper;
import com.amigo.online.provider.user.model.entity.Hobby;
import com.amigo.online.provider.user.model.entity.UserHobby;
import com.amigo.online.provider.user.service.HobbyService;
import com.amigo.online.provider.user.service.TokenManagerService;
import com.amigo.online.provider.user.util.ConvertUtil;

@Service
@Transactional(rollbackFor = Exception.class)
public class HobbyServiceImpl implements HobbyService {

    @Autowired
    RwRedisService rwRedisService;

    @Resource
    private TokenManagerService tokenManagerService;

    @Autowired
    private UserHobbyMapper userHobbyMapper;

    @Autowired
    private HobbyMapper hobbyMapper;

    private static String user_key = RedisKey.USER_REDIS_KEY;

    @Override
    public ArrayList<HobbyUserInfo> updateHobby(List<Long> hobbyList, Integer type) {
        Long nowTime = System.currentTimeMillis() / 1000;
        UserInfo nowUser = GlobalController.getNowUser();
        List<HobbyInfo> hobbys = nowUser.getHobbylist();
        ArrayList<HobbyUserInfo> hobby = new ArrayList<>();
        for (HobbyInfo hobbyInfo : hobbys) {
            if (hobbyInfo.getHobbyType().equals(type)) {
                hobby = hobbyInfo.getHobbys();
            }
        }
        List<Long> idlist = new ArrayList<>();
        if (hobby.size() > 0) {
            idlist = hobby.stream().map(e -> e.getId()).collect(Collectors.toList());
        }
        LocalUserInfo info = LocalUser.getUser();
        Long uid = nowUser.getId();
        //批量删除数据库
        if (idlist.size() > 0) {
            userHobbyMapper.updateDelete(idlist, uid, nowTime);
        }
        //批量增加爱好
        if (hobbyList.size() > 0) {
            userHobbyMapper.addHobby(hobbyList, uid, nowTime);
        }
        //改完查所有更新缓存
        ArrayList<HobbyUserInfo> result = new ArrayList<>();
        List<UserHobby> userhobby = userHobbyMapper.findHobbyByUserId(uid);
        ArrayList<HobbyInfo> hobbylist = ConvertUtil.HobbyToHobbyInfo(userhobby);
        for (HobbyInfo hobbyInfo : hobbylist) {
            if (hobbyInfo.getHobbyType().equals(type)) {
                result = hobbyInfo.getHobbys();
            }
        }
        nowUser.setHobbylist(hobbylist);
        info.setUser(nowUser);
        tokenManagerService.updateUser(info);
        rwRedisService.hput(user_key, uid, nowUser);
        return result;
    }

    @Override
    public List<Hobby> findHobby(Integer type) {
        List<Hobby> list = (ArrayList<Hobby>) hobbyMapper.findHobbyByType(type);
        return list;
    }

}
